/**
 *
 * @author ilya portnyagin iportnyagin@gmail.com
 */

package ru.portnyagin.helpdeskru.controller;

import java.io.Serializable;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import ru.portnyagin.helpdeskru.model.UserHD;
import ru.portnyagin.helpdeskru.util.JsfUtil;


@ManagedBean(name = "loginController")
@SessionScoped


public class LoginController implements Serializable {

    private UserHD logged = null;
    private String passwdNew = null;
    private String passwdNew2 = null;
    
    private String theme = "sam";
            
    private String[] themes = {
        "aristo","afterdark","afternoon","afterwork","black-tie","blitzer","bluesky","bootstrap",
        "casablanca","cruze","cupertino","dark-hive","delta","dot-luv","eggplant","excite-bike","flick",
        "glass-x","home","hot-sneaks","humanity","le-frog","midnight","mint-choc","overcast",
        "pepper-grinder","redmond","rocket","sam","smoothness","south-street","start","sunny",
        "swanky-purse","trontastic","ui-darkness","ui-lightness","vader"};

    
    @EJB private ru.portnyagin.helpdeskru.service.UserHDService userHDService;
    
    private Boolean visibleDefferedRequest = false;
    
    public LoginController() {
        
    }
    
    public String getRemoteUser() {
        return FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
    }
    
    public void login() {
        if (logged != null) {
            return;
        }
        
        String nameLogin2 = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
        
        if (nameLogin2 == null) {
            return;
        }
        
        logged = userHDService.findByLogin(nameLogin2);
        
        if(logged.getTheme() != null && !logged.getTheme().isEmpty())
            this.theme = logged.getTheme();
        
    }

    public String getTheme() {
        return theme;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }


    public String[] getThemes() {
        return themes;
    }

    public void setThemes(String[] themes) {
        this.themes = themes;
    }
    
    
     public void saveTheme() {
         logged.setTheme(this.theme);

        try {
            userHDService.edit(logged);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("property",
                FacesContext.getCurrentInstance().getViewRoot().getLocale()).
                getString("themeChanged"));
            
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e.toString());
        }
     }

    public Boolean getVisibleDefferedRequest() {
        return visibleDefferedRequest;
    }

    public void setVisibleDefferedRequest(Boolean visibleDefferedRequest) {
        this.visibleDefferedRequest = visibleDefferedRequest;
    }
    
   
    
    

     public String logout() {
        logged = null;
        
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        
        return "/index.xhtml?faces-redirect=true";
        
     }    
    
    
    public String getPasswdNew() {
        return passwdNew;
    }

    public void setPasswdNew(String passwdNew) {
        this.passwdNew = passwdNew;
    }

    public String getPasswdNew2() {
        return passwdNew2;
    }

    public void setPasswdNew2(String passwdNew2) {
        this.passwdNew2 = passwdNew2;
    }

    public UserHD getLogged() {
        
        if (logged == null) {
            this.login();
        }
        
        return logged;
    }
        
    public boolean isIsLogged() {
        if (logged == null) {
            return false;
        } else {
            return true;
        }
    }
    
    

    public void saveNewPasswd() {

        String pass, pass2;
        
        if(this.passwdNew != null)
            pass = this.passwdNew;
        else 
            pass = "";
        
        if(this.passwdNew2 != null)
            pass2 = this.passwdNew2;
        else 
            pass2 = "";

        if (!pass.equals(pass2) ) {
            JsfUtil.addErrorMessage(ResourceBundle.getBundle("property",
                FacesContext.getCurrentInstance().getViewRoot().getLocale()).
                getString("passwordsNotMatch"));
            
            return;
        }
            
        logged.setPassword(pass);

        try {
            userHDService.edit(logged);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("property",
                FacesContext.getCurrentInstance().getViewRoot().getLocale()).
                getString("passwordChanged"));            
            
            
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e.toString());
        }

        passwdNew = null;
        passwdNew2 = null;
    }




}
